package com.wnx.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.wnx.db.SaleDao;
import com.wnx.entity.Admin;
import com.wnx.entity.Goods;
import com.wnx.entity.GoodsPager;
import com.wnx.entity.Menu;
import com.wnx.service.FirstService;
import com.wnx.service.SaleService;

public class SaleAction extends ActionSupport {
	private Menu menu;
	private int page=1;
	private String opt;
	private String search;
	
	public String getSearch() {
		return search;
	}
	public void setSearch(String search) {
		this.search = search;
	}
	public String getOpt() {
		return opt;
	}
	public void setOpt(String opt) {
		this.opt = opt;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public Menu getMenu() {
		return menu;
	}
	public void setMenu(Menu menu) {
		this.menu = menu;
	}
	public String execute() throws Exception{
		String opt = this.opt;
		if(opt.equals("all")){
			SaleService ss = new SaleService();
			List<Menu> menulist = ss.getMenuAll();
		
			if(page<0){
				page=1;
			}
			
			SaleDao sd = new SaleDao();
			GoodsPager g = sd.getGoodsByPager(page-1);
			Map<String, Object> session = ActionContext.getContext().getSession();
			session.put("menulist", menulist);
			session.put("opt", opt);
			session.put("g", g);
			return SUCCESS;
		}else if(opt.equals("cls")){
			HttpServletRequest request = ServletActionContext.getRequest();
			int mid = Integer.parseInt(request.getParameter("mid"));
			if(page<0){
				page=1;
			}
			
			SaleDao sd = new SaleDao();
			GoodsPager g = sd.getGoodsByPagerBymid(page-1, mid);
			Map<String, Object> session = ActionContext.getContext().getSession();
			session.put("g", g);;
			session.put("opt", opt);
			return SUCCESS;
		}else if(opt.equals("search")){
			if(page<0){
				page=1;
			}
			
			SaleDao sd = new SaleDao();
			GoodsPager g = sd.getGoodsByPagerSearch(page-1, this.search);
			Map<String, Object> session = ActionContext.getContext().getSession();
			session.put("g", g);
			session.put("opt", opt);
			return SUCCESS;
		}else {
			
			return SUCCESS;
		}
	}
}
